/**create by framework at 2019年05月30日 15:47:03**/
package com.star.truffle.module.fzjz.controller.api;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.star.truffle.common.constants.EnabledEnum;
import com.star.truffle.core.StarServiceException;
import com.star.truffle.core.annotation.IgnoreSecurity;
import com.star.truffle.core.eureka.StarGateway;
import com.star.truffle.core.web.ApiCode;
import com.star.truffle.core.web.ApiResult;
import com.star.truffle.module.fzjz.dto.req.WarCategoryRequestDto;
import com.star.truffle.module.fzjz.dto.res.WarCategoryResponseDto;
import com.star.truffle.module.fzjz.service.WarCategoryService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import springfox.documentation.annotations.ApiIgnore;

@Slf4j
@RestController
@RequestMapping("/api/war/category")
@Api(tags = "WarCategoryApiController")
public class WarCategoryApiController {

  @Autowired
  private WarCategoryService warCategoryService;

  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/info", method = RequestMethod.POST)
  @ApiOperation(value = "根据主键获取作战图分类", notes = "根据主键获取作战图分类", httpMethod = "POST", response = WarCategoryResponseDto.class)
  @ApiImplicitParams({
    @ApiImplicitParam(name = "cateId", value = "分类id", dataType = "Long", required = true, paramType = "query")
  })
  public ApiResult<WarCategoryResponseDto> getWarCategory(Long cateId) {
    try {
      WarCategoryResponseDto warCategoryResponseDto = warCategoryService.getWarCategory(cateId);
      return ApiResult.success(warCategoryResponseDto);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }

  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/list", method = RequestMethod.POST)
  @ApiOperation(value = "根据条件获取作战图分类列表", notes = "根据条件获取作战图分类列表", httpMethod = "POST")
  @ApiImplicitParams({
    @ApiImplicitParam(name = "parentId", value = "父级分类", dataType = "Long", required = false, paramType = "query"),
  })
  public ApiResult<List<WarCategoryResponseDto>> queryWarCategory(@ApiIgnore WarCategoryRequestDto warCategoryRequestDto) {
    try {
      warCategoryRequestDto.setDisabledCate(0);
      warCategoryRequestDto.setEnabled(EnabledEnum.enabled.val());
      List<WarCategoryResponseDto> list = warCategoryService.queryWarCategory(warCategoryRequestDto);
      list = list.stream().filter(item -> item.getChildNum() > 0 || item.getParentId() > 0).collect(Collectors.toList());
      return ApiResult.success(list);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }

}